[1] 2
How to code efficiently
25-Nov-2025
| Purpose | Writing a Paper | Writing Code |
|---|---|---|
| Plan and organize your ideas/approach | Create an outline | Write pseudocode |
| Communicate your ideas | Write the actual text | Write the code |
| Get feedback | proofread, share with a co-author | Test code or write unit tests |
| Facilitate communication and readability with standards | Use standard conventions of grammar and spelling | Use a style guide to keep code consistent |
| Edit, edit, edit | Refine, polish | Improve efficiency, clarity, and documentation |
# Plot ordinations
# Step 1: Read in data
# Step 2: re-format data
# Step 3: Calculate ordination
# Calculating bray-curtis dissimilarities
sb_transformed <- t(sqrt(input$data_loaded))
dm_bc <- vegdist(sb_transformed, method = "bray")
# NMDS ordination (only for bray-curtis dissimilarity)
sb.nmds <- metaMDS(dm_bc, k = 2, trymax = 100)
# Step 4: Plot ordination
# Step 5 save graphs, and distance matrixpurrr, apply and similar functions)Example: I need to filter based on the same group of samples or OTUs
Example: I need to filter based on the same group of samples or OTUs
I want all of my plots to have similar elements
plot1 <- genus_10000_contigs_derep95_bins %>%
ggplot(aes(habitat, percent_assembled)) +
stat_summary(fun=mean, geom="bar", colour=colour_brewer$grey, fill=colour_brewer$blue, size=1),
stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom="errorbar", size=1, colour=colour_brewer$grey, width=0.5),
scale_x_discrete(breaks = habitat_levels, labels = habitat_labels),
scale_y_continuous(expand = expansion(mult = c(0, .1)), limits = c(0, 100)),
theme_cowplot() +
ylab("Average percent assembled")
plot2 <- genus_2500_contigs_all_bins %>%
ggplot(aes(habitat, percent_assembled)) +
stat_summary(fun=mean, geom="bar", colour=colour_brewer$grey, fill=colour_brewer$blue, size=1),
stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom="errorbar", size=1, colour=colour_brewer$grey, width=0.5),
scale_x_discrete(breaks = habitat_levels, labels = habitat_labels),
scale_y_continuous(expand = expansion(mult = c(0, .1)), limits = c(0, 100)),
theme_cowplot() +
ylab("Average percent assembled")bar_plot_layers <- list(
stat_summary(fun=mean, geom="bar", colour=colour_brewer$grey, fill=colour_brewer$blue, size=1),
stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom="errorbar", size=1, colour=colour_brewer$grey, width=0.5),
scale_x_discrete(breaks = habitat_levels, labels = habitat_labels),
scale_y_continuous(expand = expansion(mult = c(0, .1)), limits = c(0, 100)),
theme_cowplot()
)
plot1 <- genus_10000_contigs_derep95_bins %>%
ggplot(aes(habitat, percent_assembled)) +
bar_plot_layers +
ylab("Average percent assembled")
plot2 <- genus_2500_contigs_all_bins %>%
ggplot(aes(habitat, percent_assembled)) +
bar_plot_layers +
ylab("Average percent assembled")I have several different data tables I need to convert to Bray-Curtis distance matrices
I have several different data tables I need to convert to Bray-Curtis distance matrices
https://mitcommlab.mit.edu/broad/commkit/coding-and-comment-style/
pipeline basics (automating repetative tasks with make files, functions, and nextflow?)
[1] 2
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).
Comments should not duplicate code